@{
    ViewData["Title"] = "销售订单明细";
    Layout = null;
}

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>销售订单明细</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #fff;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
            position: relative;
        }

        .logo {
            position: absolute;
            left: 0;
            top: 0;
            width: 60px;
            height: 60px;
            background-color: #1E9FFF;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            font-size: 18px;
        }

        .title {
            font-size: 24px;
            font-weight: bold;
            margin: 0;
        }

        .qr-code {
            position: absolute;
            right: 0;
            top: 0;
            width: 80px;
            height: 80px;
            background-color: #000;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 12px;
        }

        .order-info {
            margin-bottom: 30px;
        }

        .info-row {
            display: flex;
            margin-bottom: 10px;
        }

        .info-label {
            width: 100px;
            font-weight: bold;
        }

        .info-value {
            flex: 1;
        }

        .product-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 30px;
        }

        .product-table th,
        .product-table td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }

        .product-table th {
            background-color: #f5f5f5;
            font-weight: bold;
        }

        .product-table tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        .footer {
            text-align: center;
            margin-top: 50px;
        }

        .print-button {
            background-color: #6c757d;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        @@media print {
            .print-button {
                display: none;
            }

            body {
                padding: 0;
            }
        }
    </style>
</head>

<body>
    <div class="header">
        <div class="logo">IMES</div>
        <h1 class="title">销售订单明细</h1>
        <div class="qr-code">QR Code</div>
    </div>

    <div class="order-info">
        <div class="info-row">
            <span class="info-label">单据编号:</span>
            <span class="info-value" id="orderNo">XSDD202208231533250003</span>
        </div>
        <div class="info-row">
            <span class="info-label">制单人:</span>
            <span class="info-value" id="creatorName">超级管理员</span>
        </div>
        <div class="info-row">
            <span class="info-label">创建时间:</span>
            <span class="info-value" id="createTime">2022-08-23 15:33:25</span>
        </div>
    </div>

    <table class="product-table">
        <thead>
            <tr>
                <th>产品编号</th>
                <th>产品名称</th>
                <th>产品规格</th>
                <th>数量</th>
            </tr>
        </thead>
        <tbody id="productTableBody">
            <tr>
                <td>CP202207301931260002</td>
                <td>HW螺丝</td>
                <td></td>
                <td>6001</td>
            </tr>
            <tr>
                <td>CP202207301846120001</td>
                <td>衣柜</td>
                <td>100/箱</td>
                <td>801</td>
            </tr>
            <tr>
                <td>CP202207301933070003</td>
                <td>鼠标</td>
                <td></td>
                <td>1000</td>
            </tr>
        </tbody>
    </table>

    <div class="footer">
        <button class="print-button" onclick="printPage()">马上就好...</button>
    </div>

    <script>
        // 获取URL参数
        function getUrlParameter(name) {
            name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
            var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
            var results = regex.exec(location.search);
            return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
        }

        // 加载销售订单数据
        function loadSalesOrderData() {
            var orderId = getUrlParameter('id');
            if (!orderId) {
                console.log('未提供销售订单ID');
                return;
            }

            // 获取销售订单详情
            fetch('/SalesOrder/QuerySalesOrder?pageIndex=1&pageSize=1000')
                .then(response => response.json())
                .then(data => {
                    console.log('销售订单数据:', JSON.stringify(data, null, 2));
                    if (data && data.data && data.data.pageData) {
                        var salesOrder = data.data.pageData.find(item => item.id == orderId);
                        if (salesOrder) {
                            document.getElementById('orderNo').textContent = salesOrder.orderNo || '';
                            document.getElementById('creatorName').textContent = salesOrder.creatorName || '超级管理员';
                            document.getElementById('createTime').textContent = salesOrder.createTime ? new Date(salesOrder.createTime).toLocaleString() : '';
                        } else {
                            console.log('未找到销售订单ID:', orderId);
                        }
                    } else {
                        console.log('API返回数据格式不正确:', data);
                    }
                })
                .catch(error => {
                    console.error('获取销售订单失败:', error);
                });

            // 获取产品明细
            fetch('/SalesOrder/QuerySalesOrderProduct?salesOrderId=' + orderId + '&pageIndex=1&pageSize=1000')
                .then(response => response.json())
                .then(data => {
                    console.log('产品明细数据:', JSON.stringify(data, null, 2));
                    if (data && data.data && data.data.pageData) {
                        var tbody = document.getElementById('productTableBody');
                        tbody.innerHTML = '';

                        if (data.data.pageData.length === 0) {
                            tbody.innerHTML = '<tr><td colspan="4" style="text-align: center;">暂无产品明细</td></tr>';
                        } else {
                            data.data.pageData.forEach(function (product) {
                                var row = tbody.insertRow();
                                row.insertCell(0).textContent = product.productCode || '';
                                row.insertCell(1).textContent = product.productName || '';
                                row.insertCell(2).textContent = product.productSpec || '';
                                row.insertCell(3).textContent = product.quantity || '';
                            });
                        }
                    } else {
                        console.log('产品明细API返回数据格式不正确:', data);
                        document.getElementById('productTableBody').innerHTML = '<tr><td colspan="4" style="text-align: center; color: red;">数据格式错误</td></tr>';
                    }
                })
                .catch(error => {
                    console.error('获取产品明细失败:', error);
                    document.getElementById('productTableBody').innerHTML = '<tr><td colspan="4" style="text-align: center; color: red;">加载失败</td></tr>';
                });
        }

        // 打印页面
        function printPage() {
            window.print();
        }

        // 页面加载完成后执行
        document.addEventListener('DOMContentLoaded', function () {
            loadSalesOrderData();
        });
    </script>
</body>

</html>
